<?php
declare(strict_types=1);
namespace cms\office\admin\api_controller;


use cms\office\request\ProjectFormRequest;
use cms\office\service\ProjectService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Psr\Http\Message\ResponseInterface;

/**
 * Class ProjectController
 * @package cms\office\admin\api_controller
 * @Controller(prefix="api/office/admin_project")
 */
class ProjectController extends CommonController
{
    /**
     * @Inject()
     * @var ProjectService
     */
    protected ProjectService $projectService;

    /**
     * @param ProjectFormRequest $request
     * @return ResponseInterface
     * @PostMapping(path="create")
     */
    public function create(ProjectFormRequest $request): ResponseInterface
    {
        $app = $this->_getApp(true);
        $request->validate('create');
        $form_data = $request->validated();
        $form_data['app_id'] = $app->id;
        $result = $this->projectService->model->create($form_data);
        return $this->success('请求成功', $result);
    }

    /**
     * @param ProjectFormRequest $request
     * @return ResponseInterface
     * @PostMapping(path="update")
     */
    public function update(ProjectFormRequest $request): ResponseInterface
    {
        $request->validate('update');
        $form_data = $request->validated();
        $id = (int) $this->request->input('id', 0);
        $data = $this->projectService->getDataById($id, true);
        $data->fill($form_data);
        $data->save();
        return $this->success('请求成功', $data);
    }


    /**
     * @return ResponseInterface
     * @RequestMapping(path="retrieve")
     */
    public function retrieve(): ResponseInterface
    {
        $app = $this->_getApp(true);
        $where = [
            ['app_id', '=', $app->id]
        ];

        $result = $this->projectService->model->where($where)->orderByDesc('id')->paginate(10);
        foreach ($result->items() as $k => $v) {
            $v->receipt_amount;
            $v->disburse_amount;
        }
        return $this->success('请求成功', [
            'total' => $result->total(),
            'items' => $result->items()
        ]);
    }

    /**
     * @return ResponseInterface
     * @RequestMapping(path="count")
     */
    public function count(): ResponseInterface
    {
        $app = $this->_getApp(true);
        $query = $this->projectService->model->where('app_id',$app->id);
        $start_time = $this->request->input('start_time', '');
        if ($start_time) {
            $query->where('start_at', '>=', $start_time);
        }
        $finish_time = $this->request->input('finish_time', '');
        if ($start_time) {
            $query->where('finish_at', '<', $finish_time);
        }
        $amount = $query->sum('amount');
        $total = $query->count('id');
        return $this->success('请求成功', [
            'total' => $total,
            'amount' => $amount
        ]);
    }
}